//
//  BPAddressSelectViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/20.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPAddressSelectViewController.h"
#import <AMapSearchKit/AMapSearchKit.h>

@interface BPAddressSelectViewController () <AMapSearchDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

{
    AMapSearchAPI *_search;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *searchImgCenterX;

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//suggestion搜索结果
@property (strong, nonatomic) NSArray *result;
@end

@implementation BPAddressSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择地址";
    
    //初始化检索对象
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    self.textField.delegate = self;
    
    //增加一个通知，实时监听textField的内容
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChanged) name:UITextFieldTextDidChangeNotification object:nil];
//    [self.tableView registerNib:[UINib nibWithNibName:@"MYLocationTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
    if(response.tips.count == 0)
    {
        return;
    }
    
    self.result = response.tips;
    
    [self.tableView reloadData];
    //    //通过AMapInputTipsSearchResponse对象处理搜索结果
    //    NSString *strCount = [NSString stringWithFormat:@"count: %ld", (long)response.count];
    //    NSString *strtips = @"";
    //    for (AMapTip *p in response.tips) {
    //        strtips = [NSString stringWithFormat:@"%@\nTip: %@", strtips, p.description];
    //    }
    //    NSString *result = [NSString stringWithFormat:@"%@ \n %@", strCount, strtips];
    //    NSLog(@"InputTips: %@", result);
}

#pragma mark - textField内容发生变化

- (void)textFieldChanged {
    //构造AMapInputTipsSearchRequest对象，设置请求参数
    AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
    tipsRequest.city = @"成都";
    tipsRequest.keywords = self.textField.text;
    
    //发起输入提示搜索
    [_search AMapInputTipsSearch: tipsRequest];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.searchImgCenterX.constant = - self.textField.width / 2 + 15;
    self.textField.textAlignment = NSTextAlignmentLeft;
    //空出位置用于显示放大镜
    self.textField.text = @"     ";
}

#pragma mark - table view相关方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
    AMapTip *tip = self.result[indexPath.row];
    
    cell.textLabel.text = tip.name;
    cell.detailTextLabel.text = tip.address;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取得地址
    AMapTip *tip = self.result[indexPath.row];
    NSString *location = [NSString stringWithFormat:@"%@ %@", tip.name, tip.address];
    NSLog(@"lat : %f lng : %f", tip.location.latitude, tip.location.longitude);
    //pop
    [self.navigationController popViewControllerAnimated:YES];
    //通过代理逆传数据
    if ([self.delegate respondsToSelector:@selector(addressSelectViewController:selectedAddress:latitude:longtitude:)]) {
        [self.delegate addressSelectViewController:self selectedAddress:location latitude:tip.location.latitude longtitude:tip.location.longitude];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.result.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


@end
